<template>
  <el-dialog
    title="编辑"
    :visible.sync="dialogVisible"
    width="30%"
    :before-close="handleClose"
  >
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
    >
      <el-form-item label="课程ID" prop="id">
        <el-input v-model="ruleForm.id" disabled></el-input>
      </el-form-item>
      <el-form-item label="课程名称" prop="title">
        <el-input v-model="ruleForm.title"></el-input>
      </el-form-item>
      <el-form-item label="报名费" prop="fee">
        <el-input v-model.number="ruleForm.fee"></el-input>
      </el-form-item>
      <el-form-item label="上传人" prop="person">
        <el-input v-model="ruleForm.person"></el-input>
      </el-form-item>
      <el-form-item label="课程状态" prop="type" class="type">
        <el-select v-model="ruleForm.type" placeholder="请选择课程状态">
          <el-option label="上架" :value="0"></el-option>
          <el-option label="下架" :value="1"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="$parent.isShow = false">取 消</el-button>
      <el-button size="small" type="primary" @click="submitForm('ruleForm')">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { updatefun } from "../../../api/course";
export default {
  props: ["id", "row"],
  data() {
    return {
      dialogVisible: true,
      ruleForm: {
        title: "",
        fee: "",
        person: "",
        type: ""
      },
      rules: {
        title: [
          { required: true, message: "请输入课程名称", trigger: "blur" },
          {
            min: 2,
            max: 15,
            message: "长度在 2 到 15 个字符",
            trigger: "blur"
          }
        ],
        fee: [{ required: true, message: "请输入报名费", trigger: "blur" }],
        person: [
          { required: true, message: "请输入上传人", trigger: "blur" },
          { min: 2, max: 9, message: "长度在 2 到 9 个字符", trigger: "blur" }
        ],
        type: [{ required: true, message: "请选择课程状态", trigger: "change" }]
      }
    };
  },
  mounted() {
    this.ruleForm = Object.assign({}, this.row);
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          updatefun(this.ruleForm).then((res) => {
            if (res.code == 200) {
              this.$parent.getData();
              this.$parent.isShow = false;
            }
          });
        } else {
          return false;
        }
      });
    },
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then(_ => {
          done();
          this.$parent.isShow = false;
        })
        .catch(_ => {});
    }
  }
};
</script>

<style lang="less" scoped>
/deep/.el-dialog__body {
  text-align: left;
  padding-right: 50px;
  .el-form-item {
    margin-bottom: 10px;
  }
  .el-input__inner {
    height: 32px;
  }
  .type {
    float: left;
    .el-input__inner {
      width: 120px;
    }
  }
}
</style>